From b6d49825b0ceb4d573ca6640200b87e974b893c6 Mon Sep 17 00:00:00 2001 From: Aryeh Gregor Date: Fri, 18 Apr 2008 20:40:47 +0000 Subject: [PATCH] Users moving a page can now have all subpages automatically moved as well. Done by request of the people in #wikibooks; this is apparently a much-desired thing there (for, I guess, obvious reasons :) ). This took longer than I thought and is somewhat hackish in places, so it could maybe use a bit of polish. --- RELEASE-NOTES | 1 + includes/SpecialMovepage.php | 178 ++++++++++++++++++------------ languages/messages/MessagesEn.php | 6 +- maintenance/language/messages.inc | 6 +- 4 files changed, 118 insertions(+), 73 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index af5c2503e8..a98fd3c2c3 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -87,6 +87,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN text from Special:UserLogin title (new message 'nav-login-createaccount') * Say "log in / create account" if an anonymous user can create an account, otherwise just "log in", consistently across skins +* Users moving a page can now have all subpages automatically moved as well === Bug fixes in 1.13 === diff --git a/includes/SpecialMovepage.php b/includes/SpecialMovepage.php index 6c5232cec7..becf7985aa 100644 --- a/includes/SpecialMovepage.php +++ b/includes/SpecialMovepage.php @@ -30,9 +30,7 @@ function wfSpecialMovepage( $par = null ) { $f = new MovePageForm( $par ); - if ( 'success' == $action ) { - $f->showSuccess(); - } else if ( 'submit' == $action && $wgRequest->wasPosted() + if ( 'submit' == $action && $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) { $f->doSubmit(); } else { @@ -46,7 +44,7 @@ function wfSpecialMovepage( $par = null ) { */ class MovePageForm { var $oldTitle, $newTitle, $reason; # Text input - var $moveTalk, $deleteAndMove; + var $moveTalk, $deleteAndMove, $moveSubpages; private $watch = false; @@ -61,12 +59,13 @@ class MovePageForm { } else { $this->moveTalk = $wgRequest->getBool( 'wpMovetalk', true ); } + $this->moveSubpages = $wgRequest->getBool( 'wpMovesubpages', false ); $this->deleteAndMove = $wgRequest->getBool( 'wpDeleteAndMove' ) && $wgRequest->getBool( 'wpConfirm' ); $this->watch = $wgRequest->getCheck( 'wpWatch' ); } function showForm( $err, $hookErr = '' ) { - global $wgOut, $wgUser; + global $wgOut, $wgUser, $wgNamespacesWithSubpages; $ot = Title::newFromURL( $this->oldTitle ); if( is_null( $ot ) ) { @@ -144,8 +143,6 @@ class MovePageForm { $wgOut->addHTML( $errMsg ); } - $moveTalkChecked = $this->moveTalk ? ' checked="checked"' : ''; - $wgOut->addHTML( Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( 'action=submit' ), 'id' => 'movepage' ) ) . Xml::openElement( 'fieldset' ) . @@ -178,12 +175,26 @@ class MovePageForm { " ); - if ( $considerTalk ) { + if( $considerTalk ) { $wgOut->addHTML( " " . - Xml::checkLabel( wfMsg( 'movetalk' ), 'wpMovetalk', 'wpMovetalk', $moveTalkChecked ) . + Xml::checkLabel( wfMsg( 'movetalk' ), 'wpMovetalk', + 'wpMovetalk', $this->moveTalk ? ' checked="checked"' : ''). + " + " + ); + } + + if( isset( $wgNamespacesWithSubpages[$ot->getNamespace()] ) ) { + $wgOut->addHTML( " + + + " . + Xml::checkLabel( wfMsgHtml( 'move-subpages' ), + 'wpMovesubpages', 'wpMovesubpages', + $this->moveSubpages ? ' checked="checked"' : '' ) . " " ); @@ -264,26 +275,98 @@ class MovePageForm { wfRunHooks( 'SpecialMovepageAfterMove', array( &$this , &$ot , &$nt ) ) ; - # Move the talk page if relevant, if it exists, and if we've been told to - $ott = $ot->getTalkPage(); - if( $ott->exists() ) { - if( $this->moveTalk && !$ot->isTalkPage() && !$nt->isTalkPage() ) { - $ntt = $nt->getTalkPage(); - - # Attempt the move - $error = $ott->moveTo( $ntt, true, $this->reason ); - if ( $error === true ) { - $talkmoved = 1; - wfRunHooks( 'SpecialMovepageAfterMove', array( &$this , &$ott , &$ntt ) ); - } else { - $talkmoved = $error; - } + $wgOut->setPagetitle( wfMsg( 'pagemovedsub' ) ); + + $oldUrl = $ot->getFullUrl( 'redirect=no' ); + $newUrl = $nt->getFullUrl(); + $oldText = $ot->getPrefixedText(); + $newText = $nt->getPrefixedText(); + $oldLink = "[$oldUrl $oldText]"; + $newLink = "[$newUrl $newText]"; + + $wgOut->addWikiMsg( 'movepage-moved', $oldLink, $newLink, $oldText, $newText ); + + # Now we move extra pages we've been asked to move: subpages and talk + # pages. First, if the old page or the new page is a talk page, we + # can't move any talk pages: cancel that. + if( $ot->isTalkPage() || $nt->isTalkPage() ) { + $this->moveTalk = false; + } + + # Next make a list of id's. This might be marginally less efficient + # than a more direct method, but this is not a highly performance-cri- + # tical code path and readable code is more important here. + # + # Note: this query works nicely on MySQL 5, but the optimizer in MySQL + # 4 might get confused. If so, consider rewriting as a UNION. + $dbr = wfGetDB( DB_SLAVE ); + if( $this->moveSubpages ) { + $conds = array( + 'page_title LIKE '.$dbr->addQuotes( $dbr->escapeLike( $ot->getDBkey() ) . '/%' ) + .' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() ) + ); + if( $this->moveTalk ) { + $conds['page_namespace'] = array( $ot->getNamespace(), + MWNamespace::getTalk($ot->getNamespace()) ); } else { - # Stay silent on the subject of talk. - $talkmoved = ''; + $conds['page_namespace'] = $ot->getNamespace(); } } else { - $talkmoved = 'notalkpage'; + if( $this->moveTalk ) { + $conds = array( + 'page_namespace' => MWNamespace::getTalk($ot->getNamespace()), + 'page_title' => $ot->getDBKey() + ); + } else { + $conds = '0 = 1'; + } + } + + $extrapages = $dbr->select( + 'page', + array( 'page_id', 'page_namespace', 'page_title' ), + $conds, + __METHOD__ + ); + + $extraOutput = ''; + $skin =& $wgUser->getSkin(); + foreach( $extrapages as $row ) { + if( $row->page_id == $ot->getArticleId() ) { + # Already did this one. + continue; + } + + $oldPage = Title::newFromRow( $row ); + $newPageName = preg_replace( + '#^'.preg_quote( $ot->getDBKey(), '#' ).'#', + $nt->getDBKey(), + $oldPage->getDBKey() + ); + # The following line is an atrocious hack. Kill it. + $newNs = $nt->getNamespace() + ($oldPage->getNamespace() & 1); + $newPage = Title::makeTitle( $newNs, $newPageName ); + + # This was copy-pasted from Renameuser, bleh. + if ( $newPage->exists() && !$oldPage->isValidMoveTarget( $newPage ) ) { + $link = $skin->makeKnownLinkObj( $newPage ); + $extraOutput .= '
  • ' . wfMsgHtml( 'movepage-page-exists', $link ) . '
  • '; + } else { + $success = $oldPage->moveTo( $newPage, true, $this->reason ); + if( $success === true ) { + $oldLink = $skin->makeKnownLinkObj( $oldPage, '', 'redirect=no' ); + $newLink = $skin->makeKnownLinkObj( $newPage ); + $extraOutput .= '
  • ' . wfMsgHtml( 'movepage-page-moved', $oldLink, $newLink ) . '
  • '; + } else { + $oldLink = $skin->makeKnownLinkObj( $oldPage ); + $newLink = $skin->makeLinkObj( $newPage ); + $extraOutput .= '
  • ' . wfMsgHtml( 'movepage-page-unmoved', $oldLink, $newLink ) . '
  • '; + } + } + } + + if( $extraOutput !== '' ) { + $wgOut->addHTML( "" ); } # Deal with watches @@ -294,49 +377,6 @@ class MovePageForm { $wgUser->removeWatch( $ot ); $wgUser->removeWatch( $nt ); } - - # Give back result to user. - $titleObj = SpecialPage::getTitleFor( 'Movepage' ); - $success = $titleObj->getFullURL( - 'action=success&oldtitle=' . wfUrlencode( $ot->getPrefixedText() ) . - '&newtitle=' . wfUrlencode( $nt->getPrefixedText() ) . - '&talkmoved='.$talkmoved ); - - $wgOut->redirect( $success ); - } - - function showSuccess() { - global $wgOut, $wgRequest, $wgUser; - - $old = Title::newFromText( $wgRequest->getVal( 'oldtitle' ) ); - $new = Title::newFromText( $wgRequest->getVal( 'newtitle' ) ); - - if( is_null( $old ) || is_null( $new ) ) { - throw new ErrorPageError( 'badtitle', 'badtitletext' ); - } - - $wgOut->setPagetitle( wfMsg( 'pagemovedsub' ) ); - - $talkmoved = $wgRequest->getVal( 'talkmoved' ); - $oldUrl = $old->getFullUrl( 'redirect=no' ); - $newUrl = $new->getFullUrl(); - $oldText = $old->getPrefixedText(); - $newText = $new->getPrefixedText(); - $oldLink = "[$oldUrl $oldText]"; - $newLink = "[$newUrl $newText]"; - - $s = wfMsgNoTrans( 'movepage-moved', $oldLink, $newLink, $oldText, $newText ); - - if ( $talkmoved == 1 ) { - $s .= "\n\n" . wfMsgNoTrans( 'talkpagemoved' ); - } elseif( 'articleexists' == $talkmoved ) { - $s .= "\n\n" . wfMsgNoTrans( 'talkexists' ); - } else { - if( !$old->isTalkPage() && $talkmoved != 'notalkpage' ) { - $s .= "\n\n" . wfMsgNoTrans( 'talkpagenotmoved', wfMsgNoTrans( $talkmoved ) ); - } - } - $wgOut->addWikiText( $s ); } function showLogFragment( $title, &$out ) { diff --git a/languages/messages/MessagesEn.php b/languages/messages/MessagesEn.php index 26559672b8..0de5d0fc3a 100644 --- a/languages/messages/MessagesEn.php +++ b/languages/messages/MessagesEn.php @@ -2345,8 +2345,10 @@ Please choose another name.', Please merge them manually.'''", 'movedto' => 'moved to', 'movetalk' => 'Move associated talk page', -'talkpagemoved' => 'The corresponding talk page was also moved.', -'talkpagenotmoved' => 'The corresponding talk page was not moved.', +'move-subpages' => 'Move all subpages, if applicable', +'movepage-page-exists' => 'The page $1 already exists and cannot be automatically overwritten.', +'movepage-page-moved' => 'The page $1 has been moved to $2.', +'movepage-page-unmoved' => 'The page $1 could not be moved to $2.', '1movedto2' => '[[$1]] moved to [[$2]]', '1movedto2_redir' => '[[$1]] moved to [[$2]] over redirect', 'movelogpage' => 'Move log', diff --git a/maintenance/language/messages.inc b/maintenance/language/messages.inc index 5f9c58a98a..cc5c2537d6 100644 --- a/maintenance/language/messages.inc +++ b/maintenance/language/messages.inc @@ -1581,8 +1581,10 @@ $wgMessageStructure = array( 'talkexists', 'movedto', 'movetalk', - 'talkpagemoved', - 'talkpagenotmoved', + 'move-subpages', + 'movepage-page-exists', + 'movepage-page-unmoved', + 'movepage-page-moved', '1movedto2', '1movedto2_redir', 'movelogpage', -- 2.20.1